package com.cmy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cmy.pojo.User;
import com.cmy.utils.JSONUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@RestController
public class UserController {
    @RequestMapping(value = "/j1",produces = "application/json;charset=utf-8")
    //@ResponseBody //不走视图解析器的注解,会直接返回字符串
    public String json01() throws JsonProcessingException {
        //Jackson ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user = new User(1, "通天塔", 18);
        String str = mapper.writeValueAsString(user);
        return str;
    }
    @RequestMapping("/j2")
    public String json02() throws JsonProcessingException {
        //ObjectMapper mapper = new ObjectMapper();
        ArrayList<User> users = new ArrayList<User>();
        User user1 = new User(1, "明天01", 18);
        User user2 = new User(2, "明天02", 18);
        User user3 = new User(3, "明天03", 18);
        User user4 = new User(4, "明天04", 18);
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);

        //String str = mapper.writeValueAsString(users);
        //return str;
        return JSONUtils.getJSON(users);

    }
    @RequestMapping("/j3")
    public String json03() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return mapper.writeValueAsString(sdf.format(date));
    }
    @RequestMapping("/j4")
    public String json04() throws JsonProcessingException {
        //ObjectMapper mapper = new ObjectMapper();
        ////不使用用时间戳
        //mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //mapper.setDateFormat(sdf);
        Date date = new Date();
        return JSONUtils.getJSON(date,"yyyy-MM-dd HH:mm:ss");
    }
    @RequestMapping("/j5")
    public String json05(){
        ArrayList<User> users = new ArrayList<User>();
        User user1 = new User(1, "明天01", 18);
        User user2 = new User(2, "明天02", 18);
        User user3 = new User(3, "明天03", 18);
        User user4 = new User(4, "明天04", 18);
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        //Java对象转JSON字符串
        String str1 = JSON.toJSONString(users);
        System.out.println("Java对象转JSON字符串:"+str1);
        //JSON字符串转Java对象
        String str2 = JSON.toJSONString(user1);
        User user = JSON.parseObject(str2,User.class);
        System.out.println("JSON字符串转Java对象"+user);
        //Java对象转JSON对象
        JSONObject jo = (JSONObject) JSON.toJSON(user2);
        System.out.println("Java对象转JSON对象"+jo.getString("id"));
        //JSON对象转Java对象
        User user5 = JSON.toJavaObject(jo, User.class);
        System.out.println("JSON对象转Java对象"+user5);


        return "fastjsontest";
    }



}

    